package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper cartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        // 创建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();

        // 搬运数据
        BeanUtils.copyProperties(dto,shoppingCart);

        // 补充用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 判断添加的是彩屏还是套餐
        if(dto.getDishId() != null) {
            // 添加的是菜品,拿菜品的id去查询得到菜品信息
            Dish dish = dishMapper.findById(dto.getDishId());
            // 补数据
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }else {
            // 添加的是套餐,拿套餐id去查询得到套餐信息
            Setmeal setmeal = setmealMapper.findById(dto.getSetmealId());

            // 补数据
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }

        // 根据用户id和菜品id或者套餐id去查询有没有添加菜品或者套餐
        ShoppingCart  shoppingCart1 = cartMapper.findByCondition(shoppingCart);

        if (shoppingCart1 != null){
            // 如果有,就数量+1
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            cartMapper.update(shoppingCart1);
        }else {
            // 如果没有就添加数据
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            cartMapper.add(shoppingCart);

        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        return cartMapper.findByUid(BaseContext.getCurrentId());
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        cartMapper.clean(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中的一个商品
     *
     * @param shoppingCart
     */
    @Override
    public void delete(ShoppingCart shoppingCart){
        ShoppingCart cart = shoppingCartMapper.findByCondition(shoppingCart);
        // 判断数量大于1还是等于1
        if (cart.getNumber() == 1){
            // 数量为1直接删除整条数据
            shoppingCartMapper.delete(cart);
        }else {
            // 数量大于1,就数量-1
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.update(cart);
        }
    }
}
